package com.syedu.cels.controller;

import com.syedu.cels.entity.Exam;
import com.syedu.cels.entity.Word;
import com.syedu.cels.entity.Wrong;
import com.syedu.cels.service.BaseService;
import com.syedu.cels.util.InputUtil;
import com.syedu.cels.view.Ui;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author easonsy
 * @Description
 * @createTime 2022/03/30 23:29:00
 */
public class App {
    public static void main(String[] args) {
        Ui ui = new Ui();
        BaseService service = new BaseService();
        String choice;
        for (; ; ) {
            ui.showMainMenu();
            choice = InputUtil.getStr("请您选择>");
            if ("1".equals(choice)) {
                for (; ; ) {
                    ui.showBaseMenu();
                    choice = InputUtil.getStr("请您选择>");
                    if ("1".equals(choice)) {
                        Map<Character, List<Word>> wordsMap = service.getWordsMap();
                        Set<Character> characters = wordsMap.keySet();
                        int newLine = 0;
                        for (Character c : characters) {
                            System.out.print(Character.toUpperCase(c) + "[" + wordsMap.get(c).size() + "]" + "\t");
                            newLine++;
                            if (newLine % 10 == 0) {
                                System.out.println();
                            }
                        }
                        System.out.println();
                        String firstLetter = InputUtil.getStr("请您输入想要查看的单词的首字母...");
                        int totalPage = (int) Math.ceil(wordsMap.get(firstLetter.charAt(0)).size() / 10);
                        int pageNow = 1;
                        for (; ; ) {
                            service.findWordsByPage(firstLetter, pageNow, 10).forEach(System.out::println);
                            if (pageNow > totalPage) {
                                System.out.println("已到达尾页...");
                                break;
                            }
                            choice = InputUtil.getStr("是否查看下一页y/n");
                            if ("y".equalsIgnoreCase(choice)) {
                                pageNow++;
                            } else {
                                break;
                            }
                        }

                    } else if ("2".equals(choice)) {
                        int vocabularyCount = service.vocabularyCount();
                        int totalPage = (int) Math.ceil(vocabularyCount / 10.0);
                        System.out.println("总词汇个数" + vocabularyCount + "总页数" + totalPage);
                        int pageNow = 1;
                        for (; ; ) {
                            service.findVocabulariesByPage(pageNow, 10).forEach(System.out::println);
                            if (pageNow > totalPage) {
                                System.out.println("已到达尾页...");
                                break;
                            }
                            choice = InputUtil.getStr("是否显示下一页");
                            if ("y".equalsIgnoreCase(choice)) {
                                pageNow++;
                            } else {
                                break;
                            }
                        }

                    } else if ("0".equals(choice)) {
                        break;
                    } else {
                        System.out.println("对不起,您的输入有误,请您重新输入....");
                    }
                }
            } else if ("2".equals(choice)) {
                for (; ; ) {
                    ui.showTestMenu();
                    choice = InputUtil.getStr("请您选择>");
                    int rightCount = 0;
                    String comment = null;
                    if ("1".equals(choice)) {
                        List<Exam> exam = service.TestExam();
                        for (Exam e : exam) {
                            System.out.println(e.getEn());
                            String answer = InputUtil.getStr("输入中文翻译");
                            if (answer.equals(e.getCns())) {
                                System.out.println("回答正确");
                                rightCount++;
                            } else {
                                System.out.println("回答错误");
                                service.wrongTest(new Wrong(e,3));
                            }
                        }
                        if (rightCount > 6) {
                            comment = "及格";
                        } else {
                            comment = "不及格";
                        }
                        System.out.println("一共10题,正确" + rightCount + "题,错误" + (10 - rightCount) + "题,正确率" + (rightCount / 10.0) + ",评价:" + comment);
                        service.storeResult(rightCount);
                    } else if ("2".equals(choice)) {
                        List<Exam> exam = service.TestExam();
                        for (Exam e : exam) {
                            System.out.println(e.getCns());
                            String answer = InputUtil.getStr("输入英文翻译");
                            if (answer.equals(e.getEn())) {
                                System.out.println("回答正确");
                                rightCount++;
                            } else {
                                System.out.println("回答错误");
                                service.wrongTest(new Wrong(e,3));
                            }
                        }
                        if (rightCount > 6) {
                            comment = "及格";
                        } else {
                            comment = "不及格";
                        }
                        System.out.println("一共10题,正确" + rightCount + "题,错误" + (10 - rightCount) + "题,正确率" + (rightCount / 10.0) + ",评价:"+comment);
                        service.storeResult(rightCount);
                    } else if ("3".equals(choice)) {
                            service.showResult();
                    } else {
                        break;
                    }
                }

            } else if ("3".equals(choice)) {
                for(;;){
                    ui.showWrongTest();
                    choice=InputUtil.getStr("请选择");
                    if("1".equals(choice)){
                        service.showWrong();
                    }else if("2".equals(choice)){
                        service.showWrongTest();
                    }else{
                        break;
                    }

                }
            } else if ("4".equals(choice)) {
                System.out.println("谢谢使用!");
                break;
            } else {
                System.out.println("参数不合法..");
            }
        }
    }
}
